/*
 *  Author: Jacky
 *  Date: 2010-7-14
 *  Description: flash plugin of jQuery
 * */
(function(G, $, window, undefined){

 	var $$ = function(path, s){
 			
 		this.instance = 'jswf_' + (new Date).getTime();

		var options = this.options = $.extend({}, $$.options, s);
		var id = this.id = options.id || this.instance;
		var container =$(options.container || "<div></div>");

		$$.CallBacks[this.instance] = {};

		var params = options.params, vars = options.vars, callBacks = options.callBacks;
		var properties = $.extend({}, {height: options.height, width: options.width}, options.properties);

		var self = this;

		for (var callBack in callBacks){
			$$.CallBacks[this.instance][callBack] = (function(option){
				return function(){
					return option.apply(self.object, arguments);
				};
			})(callBacks[callBack]);
			vars[callBack] = 'G.ui.swf.CallBacks.' + this.instance + '.' + callBack;
		}

		params.flashVars = $.param(vars);

		if ($.browser.msie){
			properties.classid = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
    		properties.codebase = 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0';
			params.movie = path;
		} else {
			properties.type = 'application/x-shockwave-flash';
			properties.data = path;
		}

		var build = ['<object id="', id ,'"'];
		
		for(var property in properties){
				build.push(' ', property , '="' , properties[property] , '"');
		}
		
		build.push('>');
		for (var param in params){
			if (params[param] !== undefined ) build.push('<param name="' , param , '" value="' , params[param] , '" />');
		}
		build.push('</object>');
		$.each(container, function(i,v){
			v.innerHTML = build.join('');;
		}); 
 		this.getObject();
 		
 	};
	$.extend($$, {
	 		options: {
				id: null,
				height: 1,
				width: 1,
				container: null,
				properties: {
					align: "middle"
				},
				params: {
					quality: 'high',
					allowScriptAccess: 'always',
					wMode: 'transparent',
					swLiveConnect: true,
					menu: false
				},
				callBacks: {},
				vars: {}
			},
	        CallBacks: {},
	        remote: function(obj, fn){
				var rs = obj.CallFunction('<invoke name="' + fn + '" returntype="javascript">' + __flash__argumentsToXML(arguments, 2) + '</invoke>');
				return eval(rs);
			}
	});
	
	$.extend($$.prototype, {
		toElement: function(){
			return this.object;
		},
		getObject: function(){
			var self = this;
			this.object = document[this.id] || window[this.id];
			if(!this.object)
				setTimeout(function(){self.getObject()}, 1);
		},
		remote: function(){
			var par = [this.toElement()];
			for(var i = 0, len = arguments.length ; i < len ; i++)
				par.push(arguments[i]);
			return $$.remote.apply($$, par);
		}
	 
	});

	$$.getVersion = function () {
	    var n = navigator;
	    if (n.plugins && n.mimeTypes.length) {
	        var plugin = n.plugins["Shockwave Flash"];
	        if (plugin && plugin.description) {
	            return plugin.description
	                    .replace(/([a-zA-Z]|\s)+/, "")
	                    .replace(/(\s)+r/, ".") + ".0";
	        }
	    } else if (window.ActiveXObject && !window.opera) {
	        for (var i = 10; i >= 2; i--) {
	            try {
	                var c = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.' + i);
	                if (c) {
	                    var version = c.GetVariable("$version");
	                    return version.replace(/WIN/g,'').replace(/,/g,'.');
	                }
	            } catch(e) {}
	        }
	    }
	};

 	G.ui.swf = $$;
 
 })(G, jQuery, window);